
> ## Color definitions ----
> col_high <- "#440154FF"

> col_low <- "#FDE725FF"

> ## PLZ, F+B, Kreis-level data ----
> load("dat/auxiliary/plz5.RData")

> load("dat/proc-data/plz5_f_und_b.RData")

> krs5_pop <- readRDS("dat/auxiliary/krs5_pop.rds")

> ## Rental market data ----
> plz5_f_und_b <- plz5_f_und_b %>%
+   group_by(plz) %>%
+   arrange(year) %>%
+   mutate(
+     rent_2005 = ifelse(length(rent[year == 2005]) == 0L,
+                        NA, rent[year == 2005]),
+     rent_2009 = ifelse(length(rent[year == 2009]) == 0L,
+                        NA, rent[year == 2009]),
+     rent_2013 = ifelse(length(rent[year == 2013]) == 0L,
+                        NA, rent[year == 2013]),
+     rent_2017 = ifelse(length(rent[year == 2017]) == 0L,
+                        NA, rent[year == 2017])
+   ) %>%
+   mutate(
+     cmr_arm_2005 = ifelse(length(cmr_arm[year == 2005]) == 0L,
+                           NA, cmr_arm[year == 2005]),
+     cmr_arm_2009 = ifelse(length(cmr_arm[year == 2009]) == 0L,
+                           NA, cmr_arm[year == 2009]),
+     cmr_arm_2013 = ifelse(length(cmr_arm[year == 2013]) == 0L,
+                           NA, cmr_arm[year == 2013])
+   ) %>%
+   mutate(
+     rent_chg_2005 = (rent - rent_2005),
+     rent_chg_2009 = (rent - rent_2009),
+     rent_chg_2013 = (rent - rent_2013),
+     rent_chg_0517 = (rent_2017 - rent_2005)
+   ) %>%
+   mutate(
+     cmr_arm_chg_2005 = (cmr_arm - cmr_arm_2005),
+     cmr_arm_chg_2009 = (cmr_arm - cmr_arm_2009),
+     cmr_arm_chg_2013 = (cmr_arm - cmr_arm_2013)
+   ) %>%
+   mutate(
+     rent_plz_mean = mean(rent, na.rm = TRUE),
+     cmr_arm_plz_mean = mean(cmr_arm, na.rm = TRUE)
+   ) %>%
+   ungroup() %>%
+   group_by(year) %>%
+   mutate(
+     rent_year_mean = mean(rent, na.rm = TRUE),
+     cmr_year_plz_mean = mean(cmr_arm, na.rm = TRUE)
+   ) 

> ## National average: levels and changes, 2005-2018 ----
> p1 <- plz5_f_und_b %>%
+   filter(year == 2018) %>%
+   ggplot() +
+   theme_dark() +
+   geom_sf(aes(fill = rent_plz_mean,
+               color = rent_plz_mean),
+           lwd = 0) +
+   scale_fill_viridis(direction = -1,
+                      limits = c(3, 18)) +
+   scale_color_viridis(direction = -1,
+                       limits = c(3, 18),
+                       guide = "none") +
+   labs(fill = "€/sqm") +
+   ggtitle("Average Rents, 2005-2018")

> p2 <- plz5_f_und_b %>%
+   filter(year == 2018) %>%
+   ggplot() +
+   theme_dark() +
+   geom_sf(aes(fill = rent_chg_2005,
+               color = rent_chg_2005),
+           lwd = 0) +
+   scale_fill_viridis(direction = -1,
+                      limits = c(-1, 9)) +
+   scale_color_viridis(direction = -1,
+                       limits = c(-1, 9),
+                       guide = "none") +
+   labs(fill = "€/sqm") +
+   ggtitle("Change in Rents, 2005-2018")

> ggsave(
+   filename = "rent-market-1.pdf",
+   plot = cowplot::plot_grid(p1, p2, ncol = 2),
+   path = "fig",
+   width = 8,
+   height = 5,
+   dpi = 200,
+   limitsize = FALSE
+ )

> ## Berlin: Levels and changes, 2005-2018 ----
> plz5_f_und_b_be <- plz5_f_und_b %>%
+   filter(substr(AGS05max, 1, 2) == "11")

> p1 <- plz5_f_und_b_be %>%
+   filter(year == 2018) %>%
+   ggplot() +
+   theme_dark() +
+   geom_sf(aes(fill = rent_plz_mean),
+           color = adjustcolor("gray50", alpha.f = 0.2),
+           lwd = 0.1) +
+   scale_fill_viridis(direction = -1,
+                      limits = c(3, 18)) +
+   scale_color_viridis(direction = -1,
+                       limits = c(3, 18),
+                       guide = "none") +
+   scale_x_continuous(breaks = seq(13, 13.8, .2)) +
+   scale_y_continuous(breaks = seq(52.3, 52.7, .1)) +
+   labs(fill = "€/sqm") +
+   ggtitle("Average Rents, 2005-2018")

> p2 <- plz5_f_und_b_be %>%
+   filter(year == 2018) %>%
+   ggplot() +
+   theme_dark() +
+   geom_sf(aes(fill = rent_chg_2005),
+           color = adjustcolor("gray50", alpha.f = 0.2),
+           lwd = 0.1) +
+   scale_fill_viridis(direction = -1,
+                      limits = c(-1, 9)) +
+   scale_color_viridis(direction = -1,
+                       limits = c(-1, 9),
+                       guide = "none") +
+   scale_x_continuous(breaks = seq(13, 13.8, .2)) +
+   scale_y_continuous(breaks = seq(52.3, 52.7, .1)) +
+   labs(fill = "€/sqm") +
+   ggtitle("Change in Rents, 2005-2018")

> ggsave(
+   filename = "rent-market-by-bula-1.pdf",
+   plot = cowplot::plot_grid(p1, p2, ncol = 2),
+   path = "fig",
+   width = 8,
+   height = 3,
+   dpi = 200,
+   limitsize = FALSE
+ )

> ## Average trends by county type ----
> plz5_f_und_b <- plz5_f_und_b %>%
+   left_join(krs5_pop, by = c("AGS05max" = "krs", "year"))

> annual <- plz5_f_und_b %>%
+   ungroup() %>%
+   st_set_geometry(NULL) %>%
+   filter(year >= 2005) %>%
+   filter(!is.na(rent) & !is.na(pop)) %>%
+   group_by(year) %>%
+   summarize(rent = weighted.mean(rent, w = pop, na.rm = T),
+             krs_type = "National Average")

> by_type <- plz5_f_und_b %>%
+   ungroup() %>%
+   st_set_geometry(NULL) %>%
+   filter(year >= 2005) %>%
+   filter(!is.na(krs_type)) %>%
+   group_by(year, krs_type) %>%
+   summarize(rent = mean(rent, na.rm = T))

> trends <- bind_rows(annual,
+                     by_type) %>%
+   group_by(krs_type) %>%
+   mutate(rent_2018 = rent[year == 2018]) %>%
+   ungroup () %>%
+   arrange(rent_2018, year) %>%
+   mutate(krs_type = as.factor(krs_type))

> sorted_krs_type <- trends %>%
+   dplyr::select(krs_type, rent_2018) %>%
+   distinct() %>%
+   arrange(rent_2018)

> trends <- trends %>%
+   mutate(krs_type = reorder(krs_type,-rent_2018))

> ## Plot
> p <- trends %>%
+   ggplot(aes(x = year, y = rent)) +
+   geom_line(aes(color = krs_type),
+             size = 1) +
+   labs(color = "Locality type") +
+   scale_color_viridis_d() +
+   xlab("Year") +
+   ylab("Rent (€/sqm)")

> ggsave(
+   filename = "rent-market-trends-1.pdf",
+   plot = p,
+   path = "fig",
+   width = 6,
+   height = 4,
+   dpi = 200,
+   limitsize = FALSE
+ )

> ## Rent prices: Levels and changes ----
> p1 <- ggpubr::ggscatter(
+   data = plz5_f_und_b %>%
+     dplyr::filter(year == 2018) %>%
+     mutate(
+       cmr_arm = ifelse(is.na(cmr_arm), rent, cmr_arm),
+       cmr_arm_chg_2005 = ifelse(is.na(cmr_arm_chg_2005), rent_chg_2005, cmr_arm_chg_2005)
+     ),
+   x = "cmr_arm",
+   y = "cmr_arm_chg_2005",
+   alpha = .05,
+   col = "gray50",
+   add = "loess",
+   add.params = list(
+     color = "black",
+     fill = "lightgray",
+     alpha = 0.5
+   ),
+   conf.int = TRUE
+ ) +
+   stat_cor(
+     method = "pearson",
+     label.x = 15,
+     label.y = -12.5,
+     r.digits = 2,
+     p.digts = 3
+   ) +
+   xlim(0, 30) +
+   ylim(-15, 15) +
+   xlab("Market Rents, 2018") +
+   ylab("Change in Market Rents, 2005-2018")

> ggsave(
+   filename = "rent-levels-and-changes-2.pdf",
+   plot = p1,
+   path = "fig",
+   width = 7,
+   height = 7,
+   dpi = 200,
+   limitsize = FALSE
+ )

> ## Rent prices and property prices: Correlation (2018) ----
> market_predictions <- readRDS("dat/auxiliary/market_predictions.rds")

> p2 <- ggpubr::ggscatter(
+   data = market_predictions,
+   x = "price",
+   y = "rent",
+   alpha = .05,
+   col = "gray50",
+   add = "loess",
+   add.params = list(
+     color = "black",
+     fill = "lightgray",
+     alpha = 0.5
+   ),
+   conf.int = TRUE
+ ) +
+   stat_cor(
+     method = "pearson",
+     label.x = 10000,
+     label.y = 5,
+     r.digits = 2,
+     p.digts = 3
+   ) +
+   xlim(0, 16000) +
+   ylim(0, 28) +
+   xlab("Property prices (EUR/sqm)") +
+   ylab("Market rents (EUR/sqm)")

> ggsave(
+   filename = "rents-and-property-prices-1.pdf",
+   plot = p2,
+   path = "fig",
+   width = 7,
+   height = 7,
+   dpi = 200,
+   limitsize = FALSE
+ )
